//
//  ZYGlobal.m
//  ZYOils
//
//  Created by WANGGNIT on 2019/9/3.
//  Copyright © 2019 WANGGNIT. All rights reserved.
//

#import "ZYGlobal.h"

@implementation ZYGlobal

+ (ZYGlobal *)shareInstance
{
    static ZYGlobal *_sharedInstance = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _sharedInstance = [[ZYGlobal alloc] init];
    });
    return _sharedInstance;
}


+(BOOL)isEqualNull:(id)value{
    if (value == nil) {
        return YES;
    }
    if (value == [NSNull null]) {
        return  YES;
    }
    if ([value isKindOfClass:[NSString class]]) {
        //stringByTrimmingCharactersInSet 去除首尾空格
        NSString *cleanString = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        
        if ([cleanString length] == 0) {
            return YES;
        }
        if ([cleanString isEqualToString:@""]) {
            return YES;
        }
        if ([cleanString containsString:@"<null>"]) {
            return YES;
        }
        if ([cleanString containsString:@"(null)"]) {
            return YES;
        }
    }
    
    return NO;
}

/**
 返回空的字符串 @""
 */
+(NSString *)isReturnKongString:(id)value
{
    
    if (value == nil) {
        return @"";
    }
    if (value == [NSNull null]) {
        return  @"";
    }
    
    return @"";
    
}

- (NSString *)returnJiTian:(NSString *)timeStampStr
{
    //1457163676000
    long long tianShu  =  [timeStampStr longLongValue]/1000;
    
    NSDate *date = [NSDate date];
    long nowTime =  [date timeIntervalSince1970];
    
    //获得相差多少秒
    long long b = tianShu-nowTime;
    
    //把相差的时间戳转化为多少天相差
    long long   c = b/(60*60*24);
    
    return [NSString stringWithFormat:@"%lld",c];
    
}

//
- (NSString *)returnJiTianQian:(NSString *)timeStampStr
{
    //1457163676000
    long long tianShu  =  [timeStampStr longLongValue]/1000;
    
    NSDate *date = [NSDate date];
    long nowTime =  [date timeIntervalSince1970];
    
    //获得相差多少秒
    long long b = nowTime-tianShu;
    
    //把相差的时间戳转化为多少天相差
    long long   c = b/(60*60*24);
    
    return [NSString stringWithFormat:@"%lld",c];
    
}


- (NSString *)returnQian:(NSString *)timeStampStr
{
    
    //获得返回的时间戳
    long long  returnTime = [timeStampStr longLongValue]/1000;
    
    //创建时间格式化器
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    //获得当前的时间
    NSDate *date = [NSDate date];
    
    [formatter setDateFormat:@"YYYY-MM-dd"];
    
    //d得到当前的日期的凌晨12点
    NSString *noTime = [NSString stringWithFormat:@"%@ 00:00:00",[formatter stringFromDate:date]];
    
    
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    //把获得的当前日期的凌晨12点转化为nsdate
    NSDate *nowDay = [formatter dateFromString:noTime];
    
    
    //获得当前的时间戳
    long nowTime =  [nowDay timeIntervalSince1970];
    
    
    
    //获得相差多少秒
    long long b =returnTime-nowTime;
    
    
    //把相差的时间戳转化为多少天相差
    long long   c = b/(60*60*24);
    
    
    
    return [NSString stringWithFormat:@"%lld",c];
    
}

//时间转换
- (NSString*)getTime:(NSString *)time
{
    if ( time == nil )
    {
        return nil;
    }
    NSLog(@"%@",time);
    //NSString *subString = [time substringWithRange:NSMakeRange(0, 10)];
    
    //获得返回的时间戳
    long long  returnTime = [time longLongValue]/1000;
    
    NSLog(@"%lld",returnTime);
    
    //创建时间格式化器
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    //获得当前的时间
    NSDate *date = [NSDate date];
    
    [formatter setDateFormat:@"YYYY-MM-dd"];
    
    //d得到当前的日期的凌晨12点
    NSString *noTime = [NSString stringWithFormat:@"%@ 00:00:00",[formatter stringFromDate:date]];
    
    NSLog(@"%@",noTime);
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    //把获得的当前日期的凌晨12点转化为nsdate
    NSDate *nowDay = [formatter dateFromString:noTime];
    
    NSLog(@"%@",nowDay);
    //获得当前的时间戳
    long nowTime =  [nowDay timeIntervalSince1970];
    
    NSLog(@"%ld",nowTime);
    
    //获得相差多少秒
    long long b = nowTime-returnTime;
    
    NSLog(@"%lld",b);
    //把相差的时间戳转化为多少天相差
    long long   c = b/(60*60*24);
    
    NSLog(@"%lld",c);
    
    //把返回的时间戳转化为时间
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:returnTime];
    
    if ( c < 1 )
    {
        [formatter setDateFormat:@"YYYY-MM-dd"];
        
        NSString *nowYear = [formatter stringFromDate:date];
        
        NSString *lastYear  = [formatter stringFromDate:confromTimesp];
        
        if ([nowYear isEqualToString:lastYear]) {
            
            //            [formatter setDateFormat:@" HH:mm"];
            //
            //            return [formatter stringFromDate:confromTimesp];
            
            long long currentTime = [[NSDate date] timeIntervalSince1970];
            
            long long deltaTime = currentTime - returnTime;
            
            if ( deltaTime >= 60*60 )//[1小时，当天24点）: HH小时前，1小时前~23小时前；
                
            {
                
                long long hour = deltaTime/(60*60);
                
                return [NSString stringWithFormat:@"%lld小时前", hour];
                
            }
            
            else if( deltaTime < 60*60 && deltaTime >= 60)//[1分钟，1小时)：MM分钟前，1分钟前~59分钟前；
                
            {
                
                long long min = deltaTime/60;
                
                return [NSString stringWithFormat:@"%lld分钟前", min];
                
            }
            
            else if( deltaTime < 60 && deltaTime >= 0 )//[0，1分钟）：刚刚；
                
            {
                
                return @"刚刚";
                
            }
            
            return @"";
        }else
        {
            // [formatter setDateFormat:@""];
            
            // NSString *later = [formatter stringFromDate:confromTimesp];
            
            NSString *t = @"昨天";
            
            return t;
        }
        
    }else if(c<6&&1<=c)
    {
        // [formatter setDateFormat:@""];
        
        NSString *s ;
        
        s = [NSString stringWithFormat:@"%lld天前",c+1];
        
        NSLog(@"%lld天前%@",c,s);
        
        return s;
    }else
    {
        [formatter setDateFormat:@"YYYY-MM-dd"];
        
        NSString *s = [formatter stringFromDate:confromTimesp];
        
        if (s!=nil) {
            
            NSArray *stingArr = [s componentsSeparatedByString:@"-"];
            
            s = [NSString stringWithFormat:@"%d-%d-%d",[stingArr[0] intValue],[stingArr[1] intValue],[stingArr[2] intValue]];
            
        }
        
        return s;
    }
}

//时间转换
- (NSString*)getTime2:(NSString *)time
{
    //    if ( [time isEqualToString:@""] )
    //    {
    //        return @"";
    //    }
    
    //获得返回的时间戳
    long long  tianShu = [time longLongValue]/1000;
    
    //IWLog(@"%lld",tianShu);//1456981496 1462291200000 1462291200000
    
    
    //获得当前的时间戳
    //创建时间格式化器
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    //    [formatter setDateStyle:NSDateFormatterShortStyle];
    //    [formatter setTimeStyle:NSDateFormatterNoStyle];
    
    [formatter setDateFormat:@"YYYY-MM-dd"];
    
    
    
    
    //    //把返回的时间戳转化为时间
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:tianShu];
    
    
    NSString *nowYear = [formatter stringFromDate:confromTimesp];
    
    return nowYear;
}

- (NSString*)getTime3:(NSString *)time
{
    //获得返回的时间戳
    long long  tianShu = [time longLongValue]/1000;
    
    //IWLog(@"%lld",tianShu);//1456981496 1462291200000 1462291200000
    
    
    //获得当前的时间戳
    //创建时间格式化器
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    //    [formatter setDateStyle:NSDateFormatterShortStyle];
    //    [formatter setTimeStyle:NSDateFormatterNoStyle];
    
    [formatter setDateFormat:@"MM月dd日"];
    
    
    
    
    //    //把返回的时间戳转化为时间
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:tianShu];
    
    
    NSString *nowYear = [formatter stringFromDate:confromTimesp];
    
    return nowYear;
}



- (NSString*)getTime4:(NSString *)time
{
    //获得返回的时间戳
    long long  tianShu = [time longLongValue]/1000;
    
    //IWLog(@"%lld",tianShu);//1456981496 1462291200000 1462291200000
    
    
    //获得当前的时间戳
    //创建时间格式化器
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    //    [formatter setDateStyle:NSDateFormatterShortStyle];
    //    [formatter setTimeStyle:NSDateFormatterNoStyle];
    
    [formatter setDateFormat:@"HH:mm"];
    
    
    
    
    //    //把返回的时间戳转化为时间
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:tianShu];
    
    
    NSString *nowYear = [formatter stringFromDate:confromTimesp];
    
    return nowYear;
}

- (NSString*)getTime5:(NSString *)time
{
    //获得返回的时间戳
    long long  tianShu = [time longLongValue]/1000;
    
    //IWLog(@"%lld",tianShu);//1456981496 1462291200000 1462291200000
    
    
    //获得当前的时间戳
    //创建时间格式化器
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    //    [formatter setDateStyle:NSDateFormatterShortStyle];
    //    [formatter setTimeStyle:NSDateFormatterNoStyle];
    
    [formatter setDateFormat:@"MM-dd HH:mm"];
    
    
    
    
    //    //把返回的时间戳转化为时间
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:tianShu];
    
    
    NSString *nowYear = [formatter stringFromDate:confromTimesp];
    
    return nowYear;
}

- (NSString*)getTime6:(NSString *)time
{
    //获得返回的时间戳
    long long  tianShu = [time longLongValue]/1000;
    
    //IWLog(@"%lld",tianShu);//1456981496 1462291200000 1462291200000
    
    
    //获得当前的时间戳
    //创建时间格式化器
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    //    [formatter setDateStyle:NSDateFormatterShortStyle];
    //    [formatter setTimeStyle:NSDateFormatterNoStyle];
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    
    
    
    //    //把返回的时间戳转化为时间
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:tianShu];
    
    
    NSString *nowYear = [formatter stringFromDate:confromTimesp];
    
    return nowYear;
}
@end
